package com.spa.modules.system.service;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spa.common.api.vo.Result;
import com.spa.common.utils.PasswordUtil;
import com.spa.common.utils.RedisUtil;
import com.spa.modules.base.entity.User;
import com.spa.modules.system.entity.SpaUser;
import com.spa.modules.system.mapper.SpaUserMapper;

import cn.dev33.satoken.stp.StpUtil;

/**
 * 描述: 用户
 * author: P0081447
 * date: 2022-06-25 16:34:30
 */
@Service
public class SpaUserService {

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private SpaUserMapper spaUserMapper;

    /**
     * 分页列表查询
     *
     * @param spaUser
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Result<Object> queryPageList(HttpServletRequest req, Integer pageNo, Integer pageSize) {
        Page<SpaUser> page = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<SpaUser> queryWrapper = new LambdaQueryWrapper<>();
        Page<SpaUser> articlePage = spaUserMapper.selectPage(page, queryWrapper);
        List<SpaUser> records = articlePage.getRecords();
        return Result.resultOK(records);
    }

    /**
     * 添加
     *
     * @param testTabel
     * @return
     */
    @Transactional
    public Result<Object> insert(SpaUser spaUser) {
        spaUser.setUsrPwd(PasswordUtil.encrypt(spaUser.getUsrName(), "123456"));
        spaUserMapper.insert(spaUser);
        return Result.resultOK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param spaUser
     * @return
     */
    @Transactional
    public Result<Object> saveById(SpaUser spaUser) {
        spaUserMapper.updateById(spaUser);
        return Result.resultOK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @Transactional
    public Result<Object> deleteById(Long id) {
        spaUserMapper.deleteById(id);
        return Result.resultOK("删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    public Result<Object> selectById(Long id) {
        SpaUser spaUser = spaUserMapper.selectById(id);
        return Result.resultOK(spaUser);
    }

    /**
     * 检查登录用户信息
     * 
     * @param user
     * @param device
     * @return
     */
    public Result<Object> checkSysUser(User user, String device) {
        QueryWrapper<SpaUser> queryWrapper = new QueryWrapper<>();// 条件构造器
        queryWrapper.eq("usr_name", user.getUsername()); // 方式1 直接当做条件
        queryWrapper.eq("usr_pwd", PasswordUtil.encrypt(user.getUsername(), user.getPassword())); // 方式1 直接当做条件
//      queryWrapper.eq("role_id", 1); // 方式1 直接当做条件
        SpaUser bayUser = spaUserMapper.selectOne(queryWrapper);
        // 判断登录信息是否合法
        if (bayUser != null) {
            bayUser.setUsrPwd(null);
            redisUtil.set("userInfo:" + bayUser.getId(), JSON.toJSONString(bayUser));
            // 获取当前用户id作为主键
            StpUtil.login(bayUser.getId(), device);
            user.setToken(StpUtil.getTokenValue());
            user.setRealName(bayUser.getRealName());
            return Result.resultOK(user);
        } else {
            return Result.error("登录失败");
        }
    }

    /**
     * 获取用户信息
     * 
     * @param user
     * @return
     */
    public Result<Object> findUserInfo() {
        String userId = (String) StpUtil.getLoginId();
        String bayUserJson = (String) redisUtil.get("userInfo:" + userId);
        
        SpaUser bayUser = JSONObject.parseObject(bayUserJson, SpaUser.class);
        return Result.resultOK(bayUser);
    }
}